<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.14.
   家居公司在建工地预约参观($$)*/

!defined('Q_PATH') && exit;

class Buildorder_C_Controller extends InitController{

	public function index(){
		$arrJiajucompany=$this->_oParent->_arrJiajucompany;
		$nId=intval(Q::G('id'));

		if(!$nId){
			$this->page404();
		}

		// 在建工地
		$arrWhere=array();
		$arrWhere['A.jiajubuild_status']=1;
		$arrWhere['A.jiajubuild_id']=$nId;
		$arrWhere['A.jiajucompany_id']=$arrJiajucompany['jiajucompany_id'];

		$arrBuild=Model::F_('jiajubuild','@A')
			->setColumns('A.jiajubuild_id,A.jiajubuild_title,A.jiajubuild_realspace,A.jiajubuild_starttime')
			->joinLeft(Q::C('DB_PREFIX').'jiajucompanyclass AS E','E.jiajucompanyclass_name AS space_name','A.jiajubuild_spaceid=E.jiajucompanyclass_id')
			->where($arrWhere)
			->getOne();

		if(empty($arrBuild['jiajubuild_id'])){
			$this->page404();
		}

		// 提交预约参观订单
		if($this->isPost()){
			$oOrder=new JiajubuildorderModel();
		
			// 站点属性
			$oOrder->site_id=$arrJiajucompany['site_id'];
			$oOrder->jiajucompany_id=$arrJiajucompany['jiajucompany_id'];

			$oOrder->jiajubuild_id=$arrBuild['jiajubuild_id'];
			$oOrder->jiajubuildorder_time=strtotime(Q::G('jiajubuildorder_time'));
			$oOrder->jiajubuildorder_status=0;
			$oOrder->save();
			if($oOrder->isError()){
				$this->E($oOrder->getErrorMessage());
			}else{
				Jiaju_Credit::RUN($this->_oParent->_arrJiajucompany['jiajucompany_id'],$oOrder->jiajubuildorder_id,$oOrder->create_dateline,1,0,'','jiajubuildorder');
				
				$this->A($oOrder->toArray(),'预约参观提交成功，请耐心等待商家联系您。',1);
			}
		}

		Core_Extend::getSeo($this,array(
			'title'=>"{$arrBuild['jiajubuild_title']}在建工地预约参观",
			'keywords'=>"{$arrBuild['jiajubuild_title']}在建工地预约参观",
			'description'=>"{$arrBuild['jiajubuild_title']}在建工地预约参观",
		));

		$this->assign('arrBuild',$arrBuild);
		$this->display(Jiaju_Extend::getTemplate($arrJiajucompany,'buildorder'));
	}

}
